package com.example.demo2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo2.common.api.Result;
import com.example.demo2.common.controller.BaseController;
import com.example.demo2.common.utils.Strings2;
import com.example.demo2.entity.Account;
import com.example.demo2.entity.AccountExtend;
import com.example.demo2.service.AccountExtendService;
import com.example.demo2.service.AccountService;

import cn.hutool.core.util.ObjectUtil;

/**
 * @Author 胡亚伟
 * @Date 2025-06-10
 * @version 1.0
 * @注释
 */

@RestController
@RequestMapping(path = "account/extend")
public class AccountExtendController extends BaseController {

	@Autowired
	public AccountExtendService accountExtendService;

	@Autowired
	public AccountService accountService;

	@RequestMapping(value = "/sign", method = RequestMethod.POST)
	public Result<?> sign(String qq) {

		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		AccountExtend ae = accountExtendService.findByQq(qq);

		if (ObjectUtil.isNull(ae)) {
			return fail("qq尚未绑定");
		} else {
			int res = accountExtendService.sign(ae);
			if (res == 0) {
				return fail("您今天已签到");
			} else if (res == 2) {
				return ok("签到成功，倍率2");
			} else if (res == 3) {
				return ok("签到成功，倍率3");
			} else if (res == 4) {
				return ok("签到成功，倍率5");
			} else {
				return ok("签到成功，倍率1");
			}
		}
	}

	/** 玩家输入 绑定qq 命令调用 */
	@RequestMapping(value = "/bind/qq", method = RequestMethod.POST)
	public Result<?> bindQq(String qq) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		AccountExtend ae = accountExtendService.findByQq(qq);
		if (ObjectUtil.isNull(ae)) {
			accountExtendService.bindQq(qq);
			return ok("qq绑定成功");
		} else {
			return fail("您已经绑定Qq，如需更换qq请联系管理员");
		}
	}

	/** 玩家输入 绑定账号 [账号] 命令调用 */
	@RequestMapping(value = "/bind/account", method = RequestMethod.POST)
	public Result<?> bindAccount(String qq, String account) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		AccountExtend a = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(a)) {
			Account acc = accountService.getById(account);
			AccountExtend ae = accountExtendService.findByQq(qq);
			if (ObjectUtil.isNull(ae)) {
				return fail("请先绑定qq");
			} else if (ObjectUtil.isNull(acc)) {
				return fail("您输入的账号不存在");
			} else {

				if (ae.getCanbindaccount() == 1) {
					accountExtendService.bindAccount(ae, account);
					return ok("账号绑定成功");
				} else {
					return fail("您已经绑定账号，如需更换账号请联系管理员");
				}
			}
		} else {
			return fail("当前账号已被绑定");
		}
	}

	/** 管理员输入 变更qq绑定状态 命令调用 */
	@RequestMapping(value = "/set/qqupdate", method = RequestMethod.POST)
	public Result<?> updateQqState(String qq, String account) {

		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);
		if (ObjectUtil.isNull(ae)) {
			return fail("请让玩家先绑定账号");
		} else {
			if (ae.getCanbindqq() == 0) {
				accountExtendService.updateCanBindQq(ae);
				return ok("账号：" + account + "已经可以更改QQ，请玩家使用'变更QQ [账号]'命令");
			} else {
				return fail("当前玩家，已经可以'变更QQ'不需要变更状态");
			}
		}
	}

	/** 管理员输入 变更账号绑定状态 命令调用 */
	@RequestMapping(value = "/set/accountupdate", method = RequestMethod.POST)
	public Result<?> updateAccountState(String qq, String account) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);
		if (ObjectUtil.isNull(ae)) {
			return fail("请让玩家先绑定账号");
		} else {
			if (ae.getCanbindaccount() == 0) {
				accountExtendService.updateCanBindAccount(ae);
				return ok("账号：: " + account + "已经可以更改账号，请玩家使用'绑定账号 [账号]'命令");
			} else {
				return fail("当前玩家，已经可以'绑定账号'不需要变更状态");
			}
		}
	}

	/** 管理员输入 变更qq 命令调用 */
	@RequestMapping(value = "/update/qq", method = RequestMethod.POST)
	public Result<?> bindQq(String qq, String account) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);
		if (ObjectUtil.isNull(ae)) {
			return fail("您尚未绑定账号");
		} else {
			if (ae.getCanbindqq() == 1) {
				accountExtendService.updateQq(ae, qq);
				return ok("qq变更成功");
			} else {
				return fail("您已经绑定Qq，如需更换qq请联系管理员");
			}
		}
	}

	@RequestMapping(value = "/cq", method = RequestMethod.POST)
	public Result<?> sign(String qq, Integer magnification, Integer prefix) {

		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		AccountExtend ae = accountExtendService.findByQq(qq);

		if (ObjectUtil.isNull(ae)) {
			return fail("请先绑定qq");
		} else {
			if (magnification == null) {
				return fail("请选择倍率");
			}
			if (prefix == null) {
				return fail("请选择猜拳");
			}
			int result = accountExtendService.cq(ae, magnification, prefix);
			if (result == 1) {
				return fail("您的积分不足");
			} else if (result == 2) {
				AccountExtend a = accountExtendService.findByQq(qq);
				return ok("猜拳错误，剩余签到点：" + a.getSign());
			} else {
				AccountExtend a = accountExtendService.findByQq(qq);
				return ok("猜拳正确，剩余签到点：" + a.getSign());
			}
		}
	}

	@RequestMapping(value = "/search/count", method = RequestMethod.POST)
	public Result<?> count(String qq) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		AccountExtend ae = accountExtendService.findByQq(qq);

		if (ObjectUtil.isNull(ae)) {
			return fail("请先绑定qq");
		} else {
			return ok("签到点：" + ae.getSign() + "，点券：" + ae.getCoupon() + "，猜拳积分：" + ae.getConsume() + "，成长值："
					+ ae.getExperience());
		}
	}

	@RequestMapping(value = "/search/account/count", method = RequestMethod.POST)
	public Result<?> count(String qq, String account) {
		if (Strings2.isNullOrWhiteSpace(qq)) {
			return fail("请通过机器人调用");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);
		if (ObjectUtil.isNull(ae)) {
			return fail("您输入的账号尚未绑定");
		} else {
			return ok("签到点：" + ae.getSign() + "，点券：" + ae.getCoupon() + "，猜拳积分：" + ae.getConsume() + "，成长值："
					+ ae.getExperience());
		}
	}

	@RequestMapping(value = "/recharge/sign", method = RequestMethod.POST)
	public Result<?> rechargeSign(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.rechargeSign(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/recharge/coupon", method = RequestMethod.POST)
	public Result<?> rechargeCoupon(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.rechargeCoupon(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/recharge/consume", method = RequestMethod.POST)
	public Result<?> rechargeConsume(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.rechargeConsume(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/update/sign", method = RequestMethod.POST)
	public Result<?> updateSign(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.updateSign(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/update/coupon", method = RequestMethod.POST)
	public Result<?> updateCoupon(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.updateCoupon(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/update/consume", method = RequestMethod.POST)
	public Result<?> updateConsume(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.updateConsume(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}

	@RequestMapping(value = "/update/experience", method = RequestMethod.POST)
	public Result<?> updateExperience(String account, Integer count) {

		if (Strings2.isNullOrWhiteSpace(account)) {
			return fail("请传入账号");
		}

		Account acc = accountService.getById(account);
		if (ObjectUtil.isNull(acc)) {
			return fail("您输入的账号不存在");
		}

		AccountExtend ae = accountExtendService.findByAccount(account);

		if (ObjectUtil.isNull(ae)) {
			return fail("当前账号不存在，请让玩家绑定账号");
		} else {
			AccountExtend a = accountExtendService.updateExperience(ae, count);
			return ok("账号：" + account + " 签到点：" + a.getSign() + "，点券：" + a.getCoupon() + "，猜拳积分：" + a.getConsume()
					+ "，成长值：" + a.getExperience());
		}
	}
}
